import Vue from 'vue'
import Router from 'vue-router'
import store from './store'

import Index from './views/index.vue'

import http from './common/http'

const axios = require('axios');


Vue.use(Router)

let r = new Router({
    routes: [
        {
            path: '/',
            name: 'index',
            component: Index,
            children : [
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () => import('./views/test.vue')
                },
                {
                    path: '/menuConfig',
                    name: 'menuConfig',
                    component: () => import('./views/menu/index1.vue')
                },
                {
                    path: '/roleList',
                    name: 'roleList',
                    component: () => import('./views/role/index1.vue')
                },
                {
                    path: '/modifyPassword',
                    name: 'modifyPassword',
                    component: () => import('./views/modifypassword/index1.vue')
                },
                {
                    path: '/floor',
                    name: 'floor',
                    component: () => import('./views/floor/index.vue')
                },
                {
                    path: '/building',
                    name: 'building',
                    component: () => import('./views/building/index.vue')
                },
                {
                    path: '/configpage',
                    name: 'configpage',
                    component: () => import('./views/test/configpage.vue')
                },
                {
                    path: '/interface',
                    name: 'interface',
                    component: () => import('./views/interface/index.vue')
                },
                {
                    path: '/testtable',
                    name: 'testtable',
                    meta: {
                        test : true,
                        name : 'user',
                    },
                    component: () => import('./views/test/table.vue')
                },
                {
                    path: '/setroute',
                    name: 'setroute',
                    meta: {
                        test : true,
                        name : 'page',
                    },
                    component: () => import('./views/test/table.vue')
                },
                {
                    path: '/dbtableconfig',
                    name: 'dbtableconfig',
                    component: () => import('./views/test/dbtableconfig.vue')
                },
                // {
                //     path: '/dbtableconfig',
                //     name: 'dbtableconfig',
                //     component: () => import('./views/test/dbtableconfig.vue')
                // },
            ]
        },
        
        {
            path: '/login',
            name: 'login',
            component: () => import('./views/login/index.vue')
        },
        {
            path: '/map',
            name: 'map',
            component: () => import('./views/map/index.vue')
        },
        
    ],

    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { x: 0, y: 0 }
        }
    },
})


// async function abc () {
    

// }

// abc();


r.beforeEach(async (to, from, next) => {

    if (to.meta.title) {
        document.title = to.meta.title;
    }

    if (to.meta.test) {

        http({
            fun : '/v1/pn/api/' + to.meta.name,
            requestType : 'GET',
            service : 'page',
            cb : res => {

                const json = JSON.parse(res.json).col;

                const columns = [];

                json.forEach((item, index) => {
                    columns.push({
                        title : item.title == '' ? index : item.title,
                        slot : item.asname == '' ? item.cname : item.asname,
                        show : item.show,
                    });
                });

                const setPageData = {
                    name : to.meta.name,
                    columns,
                    data : [],
                };

                store.commit('setPageData', setPageData);
                next();
            }
        })
    }else{
        next();
    }
    
});









// http({
//     fun : 'api',
//     service : 'aa1',
//     requestType : 'GET',
//     cb : res => {
//         console.log('aa1');
//         console.log(res);

//         let children = [];

//         res.forEach(item => {
//             children.push({
//                 path: '/' + item.name,
//                 name : item.name,
//                 meta: {
//                     test : true,
//                     name : item.name,
//                 },
//                 component: () => import('./views/test/table.vue')
//             });
//         })

//         r.addRoutes([
//             {
//                 path: '/',
//                 name: 'index',
//                 component: () => import('./views/index.vue'),
//                 children,
//             }
//         ]);
//     }
// });


// r.addRoutes([
//     {
//         path: '/test',
//         name: 'test',
//         component: () => import('./views/test.vue')
//     },
// ]);


export default r;
